package com.ks.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ks.dto.Tai;
import com.ks.utls.ExcelUtil;

@Controller
@RequestMapping("/index")
public class IndexController {
	
	@Autowired  
	private HttpServletRequest request; 
	
	private static String title1 = "台号";
	private static String title2 = "人名";
	private static String excelFullPath = "D:\\luck\\data.xls";
//	private static String excelFullPath = "data.xls";
	private int luckTaiTotal = 2;
	private static int luckEmpTotal = 1;
	private List<Integer> luckTaiPool = new ArrayList<Integer>();
	

	@ResponseBody
	@RequestMapping(value = "/getTaiData", method = RequestMethod.POST)
	public List<Tai> getTaiData() throws IOException {
		return packageTaiHaoListTest();
	}

	public List<Tai> packageTaiHaoListTest() throws IOException {
		List<Map> excelList = ExcelUtil.readExcel2003(excelFullPath);
		int taiHaoTotal = getTaiHaoTotal(excelList);
		List<Tai> taiList = createNullTai(taiHaoTotal);
		for (int i = 0; i < excelList.size(); i++) {
			Map map = excelList.get(i);
			Integer v1 = Integer.parseInt((String) map.get(title1));
			String v2 = (String) map.get(title2);

			Integer taiHaoSortIndex = v1 - 1;
			Tai tai = taiList.get(taiHaoSortIndex);
			tai.setTaiHao(v1);
			List<String> emps = tai.getEmps();
			emps.add(v2);
		}
		return taiList;
	}
	
	
	private int getTaiHaoTotal(List<Map> excelList) {
		Set<String> taiHaoSet = new HashSet<String>();
		for (int i = 0; i < excelList.size(); i++) {
			Map map = excelList.get(i);
			String v1 = (String) map.get(title1);
			taiHaoSet.add(v1);
		}
		return taiHaoSet.size();
	}

	private List<Tai> createNullTai(int taiHaoTotal) {
		List<Tai> taiList = new ArrayList<Tai>();
		for (int i = 0; i < taiHaoTotal; i++) {
			Tai tai = new Tai();
			ArrayList<String> itemEmps = new ArrayList<String>();
			tai.setEmps(itemEmps);
			taiList.add(tai);
		}
		return taiList;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getAllLuck", method = RequestMethod.POST)
	public Map<Integer, String> getAllLuck(Integer luckTotal) throws IOException {
		luckTaiTotal = 	luckTotal;
		
		List<Tai> taiHaoList = packageTaiHaoListTest();
		Map<Integer, String> luckMap = new HashMap<Integer, String>();

		List<Tai> luckTaiList = getLuckTaiList(taiHaoList);
		for (Tai tai : luckTaiList) {
			String luckEmp = getLuckEmp(tai.getEmps());
			System.out.println("幸运台号：" + tai.getTaiHao() + "。幸运员工：" + luckEmp);
			luckMap.put(tai.getTaiHao(), luckEmp);
		}
		return luckMap;
	}

	
	private String getLuckEmp(List<String> emps) {
		Random r2 = new Random();
		int luckEmpIndex = r2.nextInt(emps.size());
		String luckEmp = emps.get(luckEmpIndex);
		return luckEmp;
	}
	
	private List<Tai> getLuckTaiList(List<Tai> taiList) {
		List<Tai> luckTaiList = new ArrayList<Tai>();
		if (luckTaiTotal <= taiList.size()) {
			for (int i = 0; i < luckTaiTotal; i++) {
				int taiTotal = taiList.size();
				// r.nextInt(int i) 返回一个伪随机数，它是取自此随机数生成器序列的、在
				// 0（包括）和指定值（不包括）之间均匀分布的 <code>int</code> 值。
				Random r1 = new Random();
				int luckTaiIndex = r1.nextInt(taiTotal);

				luckTaiPool.add(luckTaiIndex);

				List<String> itemEmps = taiList.get(luckTaiIndex).getEmps();
				int luckTaiHao = taiList.get(luckTaiIndex).getTaiHao();
				Tai tai = new Tai();
				tai.setTaiHao(luckTaiHao);
				tai.setEmps(itemEmps);
				luckTaiList.add(tai);

				taiList.remove(luckTaiIndex);
				;
			}
		}
		return luckTaiList;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/getLuckTai", method = RequestMethod.POST)
	public Tai getLuckTai(String existLuckTaiStr, Integer luckTotal) throws IOException {
		luckTaiTotal = luckTotal;
		List<Tai> taiHaoList = packageTaiHaoListTest();
		Tai tai = getLuckTai(existLuckTaiStr, taiHaoList);
		return tai;
	}
	
	private Integer getRandom(int taiTotal){
		int max = taiTotal;
		int min = 1;
		return new Random().nextInt(max - min) + min; 
	}
	private Tai getLuckTai(String existLuckTaiStr, List<Tai> taiList) {
//		HttpSession session = request.getSession();
		int taiTotal = taiList.size();
//		System.out.println(taiList.get(0).getTaiHao());
//		List<Integer> existLuckTaiList = (List<Integer>)session.getAttribute("existLuckTaiList");
//		if(null == existLuckTaiList){
//			existLuckTaiList = new ArrayList<Integer>();
//		}
		Integer luckTaiIndex = getRandom(taiTotal);
		if(null == existLuckTaiStr || existLuckTaiStr.equals("")){
			
			List<String> itemEmps = taiList.get(luckTaiIndex).getEmps();
			int luckTaiHao = taiList.get(luckTaiIndex).getTaiHao();
			Tai tai = new Tai();
			tai.setTaiHao(luckTaiHao);
			tai.setEmps(itemEmps);
			return tai;
		}else{
			String[] existLuckTaiArr = existLuckTaiStr.split(",");
			luckTaiIndex = getLuckTaiIndex( existLuckTaiArr, taiTotal);
				List<String> itemEmps = taiList.get(luckTaiIndex).getEmps();
				int luckTaiHao = taiList.get(luckTaiIndex).getTaiHao();
				Tai tai = new Tai();
				tai.setTaiHao(luckTaiHao);
				tai.setEmps(itemEmps);
				return tai;
		}
	}

	/**
	 * 递归
	 * @param existLuckTaiArr
	 * @param taiTotal
	 * @return
	 */
	private Integer getLuckTaiIndex( String[] existLuckTaiArr, int taiTotal){
		Integer luckTaiIndex = getRandom(taiTotal);
		for (int i = 0; i < existLuckTaiArr.length; i++) {
			Integer existLuckTaiIndex =  Integer.parseInt(existLuckTaiArr[i])-1;
			// 出现已有就，重新调自己
			if(luckTaiIndex == existLuckTaiIndex){
				System.out.println("$$$$$$$$$");
				return getLuckTaiIndex(existLuckTaiArr,taiTotal);
			}
		}
		return luckTaiIndex ;
	}
}
